package com.pdg.dungeondelver.client;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import com.pdg.dungeondelver.client.constants.BuilderConstants;
import com.pdg.dungeondelver.client.constants.PropertyGroupConstants;
import com.pdg.dungeondelver.configuration.Configuration;
import com.pdg.dungeondelver.engine.AtlasBuilder;

public class MainMenuState extends BaseGameState {
	
	private final MenuItem[] menuItems ={
			new MenuItem(true,"Resume",MenuItemIdentifier.RESUME),
			new MenuItem(true,"New",MenuItemIdentifier.NEW),
			new MenuItem(true,"Save",MenuItemIdentifier.SAVE),
			new MenuItem(true,"Load",MenuItemIdentifier.LOAD),
			new MenuItem(true,"Instructions",MenuItemIdentifier.INSTRUCTIONS),
			new MenuItem(true,"Options",MenuItemIdentifier.OPTIONS),
			new MenuItem(true,"Quit",MenuItemIdentifier.QUIT),
	};
	private Integer currentMenuItem = 0;
	
	@Override
	public void enter(GameContainer gc, StateBasedGame sbg)
			throws SlickException {
		super.enter(gc, sbg);
		updateMenuItems();
	}
	private Boolean isValidMenuItem(){
		return menuItems[currentMenuItem].getEnabled();
	}
	private void nextMenuItem(){
		currentMenuItem++;
		currentMenuItem%=menuItems.length;
	}
	private void previousMenuItem(){
		currentMenuItem+=(menuItems.length-1);
		currentMenuItem%=menuItems.length;
	}
	private void updateMenuItems() {
		if(getClient().getAtlas()!=null){
			disableMenuItem(MenuItemIdentifier.RESUME);
			enableMenuItem(MenuItemIdentifier.NEW);
			disableMenuItem(MenuItemIdentifier.SAVE);
			disableMenuItem(MenuItemIdentifier.LOAD);
			disableMenuItem(MenuItemIdentifier.INSTRUCTIONS);
			disableMenuItem(MenuItemIdentifier.OPTIONS);
			enableMenuItem(MenuItemIdentifier.QUIT);
		}else{
			disableMenuItem(MenuItemIdentifier.RESUME);
			enableMenuItem(MenuItemIdentifier.NEW);
			disableMenuItem(MenuItemIdentifier.SAVE);
			disableMenuItem(MenuItemIdentifier.LOAD);
			disableMenuItem(MenuItemIdentifier.INSTRUCTIONS);
			disableMenuItem(MenuItemIdentifier.OPTIONS);
			enableMenuItem(MenuItemIdentifier.QUIT);
		}
		while(!isValidMenuItem()){
			nextMenuItem();
		}
	}
	private void disableMenuItem(MenuItemIdentifier identifier) {
		for(MenuItem menuItem:menuItems){
			if(menuItem.getIdentifier()==identifier){
				menuItem.setEnabled(false);
			}
		}
	}
	private void enableMenuItem(MenuItemIdentifier identifier) {
		for(MenuItem menuItem:menuItems){
			if(menuItem.getIdentifier()==identifier){
				menuItem.setEnabled(true);
			}
		}
	}
	public MainMenuState(Configuration theConfiguration,Client theClient,States theState) {
		super(theConfiguration,theClient,theState);
	}

	@Override
	public void keyPressed(int key, char c) {
		super.keyPressed(key, c);
		switch(key){
		case Input.KEY_UP:
			do{
				previousMenuItem();
			}while(!isValidMenuItem());
			break;
		case Input.KEY_DOWN:
			do{
				nextMenuItem();
			}while(!isValidMenuItem());
			break;
		case Input.KEY_ENTER:
			switch(menuItems[currentMenuItem].getIdentifier()){
			case RESUME:
				getClient().enterState(States.PLAY.getIndex());
				break;
			case NEW:
				getClient().setAtlas(getConfiguration().getPropertyGroups().get(PropertyGroupConstants.BUILDERS).<AtlasBuilder>get(BuilderConstants.ATLAS_BUILDER).build(getConfiguration()));
				getClient().enterState(States.PLAY.getIndex());
				break;
			case SAVE:
				break;
			case LOAD:
				break;
			case OPTIONS:
				break;
			case INSTRUCTIONS:
				break;
			case QUIT:
				getClient().getContainer().exit();
				break;
			}
			break;
		}
	}

	@Override
	public void keyReleased(int arg0, char arg1) {
		super.keyReleased(arg0, arg1);
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sbg, Graphics g)
			throws SlickException {
		super.render(gc, sbg, g);
		for(Integer index=0;index<menuItems.length;++index){
			MenuItem menuItem = menuItems[index];
			if(menuItem.getEnabled()){
				if(index==currentMenuItem){
					g.setColor(Color.white);
				}else{
					g.setColor(Color.gray);
				}
			}else{
				g.setColor(Color.darkGray);
			}
			g.drawString(menuItem.getCaption(), 0, g.getFont().getLineHeight()*index);
		}
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sbg, int delta)
			throws SlickException {
		super.update(gc, sbg, delta);
	}

}
